#ifndef __MC31xx_HAL_DEF_H
#define __MC31xx_HAL_DEF_H

#ifdef __cplusplus
extern "C" {
#endif

#include "mc31xx.h"
#include <stddef.h>


typedef enum { 
    HAL_OK = 0x00U, 
    HAL_ERROR = 0x01U, 
    HAL_BUSY = 0x02U, 
    HAL_TIMEOUT = 0x03U 
} HAL_StatusTypeDef;
typedef enum { HAL_UNLOCKED = 0x00U, HAL_LOCKED = 0x01U } HAL_LockTypeDef;
typedef enum { 
    HAL_PIN_UNUSED = 0x00U, 
    HAL_PIN_P0, 
    HAL_PIN_P1, 
    HAL_PIN_P2, 
    HAL_PIN_P3, 
    HAL_PIN_P4, 
    HAL_PIN_P5, 
    HAL_PIN_P6, 
    HAL_PIN_P7 
} HAL_PinTypeDef;

#define HAL_NO_WAIT                                 0
#define HAL_WAIT_FOREVER                            0xFFFFFFFF

#ifndef TRUE
#define TRUE    1
#define FALSE   0
#endif
#ifndef NULL
#define NULL    0
#endif

typedef int32_t  s32;
typedef int16_t s16;
typedef int8_t  s8;
typedef const int32_t sc32;  /*!< Read Only */
typedef const int16_t sc16;  /*!< Read Only */
typedef const int8_t sc8;   /*!< Read Only */
typedef __IO int32_t  vs32;
typedef __IO int16_t  vs16;
typedef __IO int8_t   vs8;
typedef __I int32_t vsc32;  /*!< Read Only */
typedef __I int16_t vsc16;  /*!< Read Only */
typedef __I int8_t vsc8;   /*!< Read Only */
typedef uint32_t  u32;
typedef uint16_t u16;
typedef uint8_t  u8;
typedef const uint32_t uc32;  /*!< Read Only */
typedef const uint16_t uc16;  /*!< Read Only */
typedef const uint8_t uc8;   /*!< Read Only */
typedef __IO uint32_t  vu32;
typedef __IO uint16_t vu16;
typedef __IO uint8_t  vu8;
typedef __I uint32_t vuc32;  /*!< Read Only */
typedef __I uint16_t vuc16;  /*!< Read Only */
typedef __I uint8_t vuc8;   /*!< Read Only */

#define __weak __attribute__ ((weak))

#define SET_BIT(REG, BIT)     ((REG) |= (BIT))
#define CLEAR_BIT(REG, BIT)   ((REG) &= ~(BIT))
#define READ_BIT(REG, BIT)    ((REG) & (BIT))
#define CLEAR_REG(REG)        ((REG) = (0x0))
#define WRITE_REG(REG, VAL)   ((REG) = (VAL))
#define READ_REG(REG)         ((REG))
#define MODIFY_REG(REG, CLEARMASK, SETMASK)  WRITE_REG((REG), (((READ_REG(REG)) & (~(CLEARMASK))) | (SETMASK)))
#define POSITION_VAL(VAL)     (__CLZ(__RBIT(VAL))) 
#define HAL_IS_BIT_SET(REG, BIT)         (((REG) & (BIT)) != 0U)
#define HAL_IS_BIT_CLR(REG, BIT)         (((REG) & (BIT)) == 0U)
#define UNUSED(X) (void)X 
#define __HAL_LOCK(__HANDLE__)                                           \
                                do{                                        \
                                    if((__HANDLE__)->Lock == HAL_LOCKED)   \
                                    {                                      \
                                       return HAL_BUSY;                    \
                                    }                                      \
                                    else                                   \
                                    {                                      \
                                       (__HANDLE__)->Lock = HAL_LOCKED;    \
                                    }                                      \
                                  }while (0U)

#define __HAL_UNLOCK(__HANDLE__)                                          \
                                  do{                                       \
                                      (__HANDLE__)->Lock = HAL_UNLOCKED;    \
                                    }while (0U)

/**********************************   全局变量   **********************************/
#include "mc31xx_hal_list.h"
//线程控制块
typedef struct {
    u8 id;                      //初始化后不会再改变
    volatile u8 lock_mux;		//客户进程写，审核进程读
    volatile u8 allow_mux;		//客户进程读，审核进程写
    volatile HAL_DL_LIST list_mux;	//由互斥锁操作
    HAL_DL_LIST list_mux_manager;	//由管理线程操作，实现互斥锁模块的均匀申请
}ThreadCB_S;

//资源管理控制怪
typedef struct {
    volatile u8 busy_mux;
    //volatile u8 owner_mux;
    ThreadCB_S* owner_mux;
    HAL_DL_LIST list_mux;
}ManagerCB_S;

//互斥锁控制块
typedef struct
{
    uint8_t         sta;       //互斥锁状态
    uint16_t        count;     //锁递归次数
    uint16_t        id;        //锁id，初始化完成之后不会再改变
    ThreadCB_S*     owner;     //持有锁的线程
    HAL_DL_LIST     list;      //锁链表，空闲时挂在未使用链表，被申请后所有其他申请此锁的线程挂在此处
} MuxCB_S;

extern uint32_t g_uwPrintfMuxHandle;
extern ThreadCB_S g_stAllThread[64];
extern ManagerCB_S g_stManager;
extern MuxCB_S  g_stAllMux[HAL_CFG_MUX_LIMIT];


#ifdef __cplusplus
}
#endif

#endif
